using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace DAL
{
    /// <summary>
    /// 接口ISuspectPointInfo的实现
    /// </summary>
    public class SuspectPointInfo : IDAL.ISuspectPointInfo
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            string strSql = "select count(*) from SuspectPointInfo where Enabled=1 and SuspectPointID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name)
        {
            string strSql = "select count(*) from SuspectPointInfo where Enabled=1 and NAME = '" + name.Trim() + "'";
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 增加一条记录
        /// </summary>
        public bool Add(Model.SuspectPointInfo model)
        {
            string strSql = "insert into SuspectPointInfo(LANDID, INSPECTUSERID, SUSPECTPOINTB, SUSPECTPOINTL, HANDLETIME, HANDLED, ENABLED, HANDLERECORDID) values(@LandID, @InspectUserID, @SuspectPointB, @SuspectPointL, @HandleTime, @Handled, @Enabled, @HandleRecordID)";
            SqlParameter LandID = new SqlParameter("LANDID", SqlDbType.Int); LandID.Value = model.LandID;
            SqlParameter InspectUserID = new SqlParameter("INSPECTUSERID", SqlDbType.Int); InspectUserID.Value = model.InspectUserID;
            SqlParameter SuspectPointB = new SqlParameter("SUSPECTPOINTB", SqlDbType.Float); SuspectPointB.Value = model.SuspectPointB;
            SqlParameter SuspectPointL = new SqlParameter("SUSPECTPOINTL", SqlDbType.Float); SuspectPointL.Value = model.SuspectPointL;
            SqlParameter HandleTime = new SqlParameter("HANDLETIME", SqlDbType.DateTime); HandleTime.Value = model.HandleTime;
            SqlParameter Handled = new SqlParameter("HANDLED", SqlDbType.Bit); Handled.Value = model.Handled;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            SqlParameter HandleRecordID = new SqlParameter("HANDLERECORDID", SqlDbType.Int); HandleRecordID.Value = model.HandleRecordID;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { LandID, InspectUserID, SuspectPointB, SuspectPointL, HandleTime, Handled, Enabled, HandleRecordID }) == 1 ? true : false;
        }

        /// <summary>
        /// 增加一条记录，返回ID
        /// </summary>
        public int Add2(Model.SuspectPointInfo model)
        {
            string strSql = "insert into SuspectPointInfo(LANDID, INSPECTUSERID, SUSPECTPOINTB, SUSPECTPOINTL, HANDLETIME, HANDLED, ENABLED, HANDLERECORDID) values(@LandID, @InspectUserID, @SuspectPointB, @SuspectPointL, @HandleTime, @Handled, @Enabled, @HandleRecordID) SELECT @@IDENTITY AS returnName;";
            SqlParameter LandID = new SqlParameter("LANDID", SqlDbType.Int); LandID.Value = model.LandID;
            SqlParameter InspectUserID = new SqlParameter("INSPECTUSERID", SqlDbType.Int); InspectUserID.Value = model.InspectUserID;
            SqlParameter SuspectPointB = new SqlParameter("SUSPECTPOINTB", SqlDbType.Float); SuspectPointB.Value = model.SuspectPointB;
            SqlParameter SuspectPointL = new SqlParameter("SUSPECTPOINTL", SqlDbType.Float); SuspectPointL.Value = model.SuspectPointL;
            SqlParameter HandleTime = new SqlParameter("HANDLETIME", SqlDbType.DateTime); HandleTime.Value = model.HandleTime;
            SqlParameter Handled = new SqlParameter("HANDLED", SqlDbType.Bit); Handled.Value = model.Handled;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            SqlParameter HandleRecordID = new SqlParameter("HANDLERECORDID", SqlDbType.Int); HandleRecordID.Value = model.HandleRecordID;
            return DBHelperSQL.GetInsertID(strSql, new SqlParameter[] { LandID, InspectUserID, SuspectPointB, SuspectPointL, HandleTime, Handled, Enabled, HandleRecordID });
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        public bool Update(Model.SuspectPointInfo model)
        {
            string strSql = "update SuspectPointInfo set LANDID = @LandID, INSPECTUSERID = @InspectUserID, SUSPECTPOINTB = @SuspectPointB, SUSPECTPOINTL = @SuspectPointL, HANDLETIME = @HandleTime, HANDLED = @Handled, ENABLED = @Enabled, HANDLERECORDID = @HandleRecordID where SuspectPointID= " + model.SuspectPointID.ToString();
            SqlParameter LandID = new SqlParameter("LANDID", SqlDbType.Int); LandID.Value = model.LandID;
            SqlParameter InspectUserID = new SqlParameter("INSPECTUSERID", SqlDbType.Int); InspectUserID.Value = model.InspectUserID;
            SqlParameter SuspectPointB = new SqlParameter("SUSPECTPOINTB", SqlDbType.Float); SuspectPointB.Value = model.SuspectPointB;
            SqlParameter SuspectPointL = new SqlParameter("SUSPECTPOINTL", SqlDbType.Float); SuspectPointL.Value = model.SuspectPointL;
            SqlParameter HandleTime = new SqlParameter("HANDLETIME", SqlDbType.DateTime); HandleTime.Value = model.HandleTime;
            SqlParameter Handled = new SqlParameter("HANDLED", SqlDbType.Bit); Handled.Value = model.Handled;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            SqlParameter HandleRecordID = new SqlParameter("HANDLERECORDID", SqlDbType.Int); HandleRecordID.Value = model.HandleRecordID;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { LandID, InspectUserID, SuspectPointB, SuspectPointL, HandleTime, Handled, Enabled, HandleRecordID }) == 1 ? true : false;
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        public bool Delete(int Id)
        {
            string strSql = "delete from SuspectPointInfo where SuspectPointID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SuspectPointInfo GetModel(int Id)
        {
            string strSql = "select * from SuspectPointInfo where SuspectPointID = " + Id.ToString();
            Model.SuspectPointInfo model = new Model.SuspectPointInfo();
            DataSet ds = DBHelperSQL.GetDataSet(strSql);
            model.SuspectPointID = Id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.SuspectPointID = Convert.ToInt32(ds.Tables[0].Rows[0]["SuspectPointID"]);
                model.LandID = Convert.ToInt32(ds.Tables[0].Rows[0]["LandID"]);
                model.InspectUserID = Convert.ToInt32(ds.Tables[0].Rows[0]["InspectUserID"]);
                model.SuspectPointB = Convert.ToDouble(ds.Tables[0].Rows[0]["SuspectPointB"]);
                model.SuspectPointL = Convert.ToDouble(ds.Tables[0].Rows[0]["SuspectPointL"]);
                model.HandleTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["HandleTime"]);
                model.Handled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Handled"]);
                model.Enabled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Enabled"]);
                model.HandleRecordID = Convert.ToInt32(ds.Tables[0].Rows[0]["HandleRecordID"]);
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SuspectPointInfo GetModel(string name)
        {
            //string strSql = "select * from SuspectPointInfo where NAME = '" + name.Trim() + "'";
            //Model.SuspectPointInfo model = new Model.SuspectPointInfo();
            //DataSet ds = DBHelperSQL.GetDataSet(strSql);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
                //model.SuspectPointID = Convert.ToInt32(ds.Tables[0].Rows[0]["SuspectPointID"]);
                //model.LandID = Convert.ToInt32(ds.Tables[0].Rows[0]["LandID"]);
                //model.InspectUserID = Convert.ToInt32(ds.Tables[0].Rows[0]["InspectUserID"]);
                //model.SuspectPointB = Convert.ToDouble(ds.Tables[0].Rows[0]["SuspectPointB"]);
                //model.SuspectPointL = Convert.ToDouble(ds.Tables[0].Rows[0]["SuspectPointL"]);
                //model.HandleTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["HandleTime"]);
                //model.Handled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Handled"]);
                //model.Enabled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Enabled"]);
                //model.HandleRecordID = Convert.ToInt32(ds.Tables[0].Rows[0]["HandleRecordID"]);
                //return model;
            //}
            //else
            //{
                //return null;
            //}
            return null;
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            string strSql = "select * from SuspectPointInfo where Enabled=1 ";
            if (strWhere.Trim() != "")
                strSql += "and " + strWhere;
            return DBHelperSQL.GetDataSet(strSql);
        }
    }
}
